<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="报警类型">
              <j-dict-select-tag
                v-model="queryParam.policeType"
                placeholder="请选择报警类型"
                dictCode="ps_police_type"
              />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="点位编号">
              <a-input v-model="queryParam.pointPosNo" placeholder="请输入点位编号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="设备编号">
              <a-input v-model="queryParam.equNo" placeholder="请输入设备编号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="设备类型">
              <j-dict-select-tag v-model="queryParam.equType" placeholder="请选择设备类型" dictCode="ps_equ_type" />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="关联农污终端">
              <a-input v-model="queryParam.terminalName" placeholder="请输入关联农污终端"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="终端编号">
              <a-input v-model="queryParam.terminalId" placeholder="请输入点位编号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="是否关闭">
              <a-select v-model="queryParam.deleted" placeholder="请选择" style="width: 100%">
                <a-select-option :value="undefined">请选择</a-select-option>
                <a-select-option :value="0">进行中</a-select-option>
                <a-select-option :value="1">已关闭</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="是否处置">
              <a-select v-model="queryParam.status" placeholder="请选择" style="width: 100%">
                <a-select-option :value="undefined">请选择</a-select-option>
                <a-select-option :value="1">未处置</a-select-option>
                <a-select-option :value="2">已处置</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :sm="8">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search" style="margin: 0 auto 10px">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <!--      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>-->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete" />删除</a-menu-item>
          <a-menu-item key="2" @click="batchClose"><a-icon type="delete" />关闭</a-menu-item>
          <a-menu-item key="3" @click="batchDispose"><a-icon type="delete" />处置</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text, record">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
          <img
            v-else
            :src="getImgView(text)"
            :preview="record.id"
            height="25px"
            alt=""
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>
        <span slot="action" slot-scope="text, record">
          <a-popconfirm title="确定关闭吗?" @confirm="() => handleClose(record)">
            <a>关闭</a>
          </a-popconfirm>
          <a-divider type="vertical" />
          <a @click="handleDispose(record)">处置</a>
        </span>
      </a-table>
    </div>

    <paishui-police-report-modal ref="modalForm" @ok="modalFormOk"></paishui-police-report-modal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import PaishuiPoliceReportModal from '@views/modules/PaishuiPoliceReport/PaishuiPoliceReportModal'
import { httpAction, getAction } from '@/api/manage'

import { filterDictTextByCache } from '@/components/dict/JDictSelectUtil'

export default {
  name: 'PaishuiPoliceReportList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    PaishuiPoliceReportModal,
  },
  data() {
    return {
      description: '报警信息管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: (t, r, index) => {
            const { pageSize, current } = this.ipagination
            return (current - 1) * pageSize + parseInt(index) + 1
          },
        },
        {
          title: '报警类型',
          align: 'center',
          dataIndex: 'policeType',
          customRender: (text, record, index) => {
            return filterDictTextByCache('ps_police_type', text)
          },
        },
        {
          title: '设备编号',
          align: 'center',
          dataIndex: 'equNo',
        },
        {
          title: '设备类型',
          align: 'center',
          dataIndex: 'equType',
          customRender: (text, record, index) => {
            return filterDictTextByCache('ps_equ_type', text)
          },
        },
        {
          title: '关联农污终端',
          align: 'center',
          dataIndex: 'terminalName',
        },
        {
          title: '终端编号',
          align: 'center',
          dataIndex: 'terminalId',
        },
        // {
        //   title: '关联组成设施',
        //   align: 'center',
        //   dataIndex: 'facilityType',
        //   customRender: (text) => {
        //     return filterDictTextByCache('nw_terminal_facility', text) || '无'
        //   },
        // },
        {
          title: '区域',
          align: 'center',
          dataIndex: 'areaId_dictText',
          customRender: (text, record, index) => {
            return text || record.areaLabel
          },
        },
        {
          title: '是否关闭',
          align: 'center',
          dataIndex: 'deleted',
          customRender: (text, record, index) => {
            return text ? '已关闭' : '进行中'
          },
        },
        {
          title: '是否处置',
          align: 'center',
          dataIndex: 'status',
          customRender: (text, record, index) => {
            return text == 2 ? '已处置' : '未处置'
          },
        },
        {
          title: '告警详情',
          align: 'center',
          dataIndex: 'detail',
        },
        {
          title: '告警时间', //new-add-cxxtodo
          align: 'center',
          dataIndex: 'createTime',
        },
        // {
        //   title: '点位编号',
        //   align: 'center',
        //   dataIndex: 'pointPosNo',
        // },
        // {
        //   title: '报警级别',
        //   align: 'center',
        //   dataIndex: 'policeLev',
        //   customRender: (text, record, index) =>
        //     text ? (text == 1 ? '一级报警' : text == 2 ? '二级报警' : text == 3 ? '三级报警' : '') : '',
        // },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/paishui/paishuiPoliceReport/list',
        delete: '/paishui/paishuiPoliceReport/delete',
        deleteBatch: '/paishui/paishuiPoliceReport/deleteBatch',
        exportXlsUrl: '/paishui/paishuiPoliceReport/exportXls',
        importExcelUrl: 'paishui/paishuiPoliceReport/importExcel',
      },
      dictOptions: {},
      superFieldList: [],
    }
  },
  created() {
    this.getSuperFieldList()
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'int', value: 'policeType', text: '报警类型', dictCode: 'ps_police_type' })
      fieldList.push({ type: 'string', value: 'position', text: '位置', dictCode: '' })
      fieldList.push({ type: 'string', value: 'pointPosNo', text: '点位编号', dictCode: '' })
      fieldList.push({ type: 'sel_depart', value: 'areaId', text: '所属区域id' })
      fieldList.push({ type: 'string', value: 'detail', text: '详情', dictCode: '' })
      fieldList.push({ type: 'string', value: 'pointPosName', text: '点位名称', dictCode: 'ps_pip_point_type' })
      fieldList.push({ type: 'int', value: 'equType', text: '设备类型', dictCode: 'ps_equ_type' })
      fieldList.push({ type: 'int', value: 'policeLev', text: '报警级别', dictCode: 'ps_police_lev' })
      this.superFieldList = fieldList
    },
    async handleDispose({ id }) {
      const res = await getAction('/paishui/paishuiPoliceReport/handleReport', { id })
      if (res.success) {
        this.$message.success('已处置成功')
        this.searchQuery()
      }
    },
    async handleClose({ id }) {
      const res = await getAction('/paishui/paishuiPoliceReport/closeReport', { id })
      if (res.success) {
        this.$message.success('关闭成功')
        this.searchQuery()
      }
    },
    batchClose() {
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！')
        return
      } else {
        var ids = this.selectedRowKeys.join(',')
        var that = this
        this.$confirm({
          title: '确认关闭',
          content: '是否关闭选中数据?',
          onOk: function () {
            that.loading = true
            getAction('/paishui/paishuiPoliceReport/batchCloseReport', { ids })
              .then((res) => {
                if (res.success) {
                  that.$message.success(res.message)
                  that.loadData()
                } else {
                  that.$message.warning(res.message)
                }
              })
              .finally(() => {
                that.loading = false
              })
          },
        })
      }
    },
    batchDispose() {
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！')
        return
      } else {
        var ids = this.selectedRowKeys.join(',')
        var that = this
        this.$confirm({
          title: '确认处置',
          content: '是否处置选中数据?',
          onOk: function () {
            that.loading = true
            getAction('/paishui/paishuiPoliceReport/batchHandleReport', { ids })
              .then((res) => {
                if (res.success) {
                  that.$message.success(res.message)
                  that.loadData()
                } else {
                  that.$message.warning(res.message)
                }
              })
              .finally(() => {
                that.loading = false
              })
          },
        })
      }
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
